package pl.diagnoser.client;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import pl.diagnoser.client.dto.AttributeDictDTO;
import pl.diagnoser.client.dto.AttributeValuesDictDTO;
import pl.diagnoser.client.dto.ObjectAttributesDTO;
import pl.diagnoser.client.dto.ObjectsDTO;
import pl.diagnoser.client.dto.SearchResultAttributesDTO;
import pl.diagnoser.client.tools.DataGetterService;
import pl.diagnoser.client.tools.DataGetterServiceAsync;
import pl.diagnoser.client.tools.DiagnoserProperties;
import pl.diagnoser.client.tools.LogIn;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.DateDisplayFormat;
import com.smartgwt.client.types.ListGridFieldType;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.ImgButton;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.CanvasItem;
import com.smartgwt.client.widgets.form.fields.DateItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.validator.LengthRangeValidator;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridField;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.layout.HLayout;

public class ObjectDetailsForm extends DynamicForm {
	
	private DataGetterServiceAsync dataGetterAsc = GWT.create(DataGetterService.class);
	private DiagnoserProperties properties = new DiagnoserProperties();
	private SelectItem objectSelector = new SelectItem();
	private TextItem objectName = new TextItem();
	private ObjectsDTO selectedObject;
	private List<ObjectsDTO> objectsList;
	private ObjectHandling objectHandling;
	List<AttributeDictDTO> attributeList;
	private HashMap<String, AttributeObject> objectsAttributeMap = new HashMap<String, AttributeObject>();
	
	ObjectDetailsForm( final ObjectsDTO objectDto, final boolean isVisible, boolean isTitleTop, final boolean refreshObjectList ) {
		super();
		//if(isVisible) {
		//	this.setNumCols(3);
		//	this.setColWidths(120, 133, "*");
		//} else {
			this.setNumCols(2);
			this.setColWidths(120, 250);
		//}
		final boolean visible;
		if(objectDto.getId() != null )
			visible = true;
		else
			visible = isVisible;
		
		initObjectSelector(isVisible, refreshObjectList);
		initObjectName(false);
		
		if(LogIn.getInstance().isObjectFieldsLoaded())
			returnMe().setFields(createFormItem(LogIn.getInstance().getObjectFormFields(), visible, false, false));
		else {
			if (dataGetterAsc == null) {
				dataGetterAsc = GWT.create(DataGetterService.class);
			}
			AsyncCallback<List<AttributeDictDTO>> callback = new AsyncCallback<List<AttributeDictDTO>>() {
				public void onFailure(Throwable caught) {
					System.out.println(caught.getMessage());
				}
	
				public void onSuccess(List<AttributeDictDTO> result) {
					LogIn.getInstance().setObjectFormFields(result);
					returnMe().setFields(createFormItem(result, visible, false, false));
					if(objectDto.getId() != null) {
						getObjectAttributes(objectDto.getId());
						returnMe().getField("objectName").setValue(objectDto.getName());
					}
				}
			};
			dataGetterAsc.getAttributeList(true, callback);
		}
		
		if(isTitleTop)
			this.setTitleOrientation(TitleOrientation.TOP);
	}

	ObjectDetailsForm(final boolean isVisibleSelector, final boolean isSearchMode, final boolean isAddingMode, final boolean refreshObjectList) {
		super();
		if(isSearchMode) {
			this.setNumCols(4);
			this.setColWidths(100, 130, 110, 50);
			this.setWidth(390);
		} else {
			this.setNumCols(3);
			this.setColWidths(120, 133, "*");
			this.setWidth(320);
		}
		this.setHeight100();
		initObjectSelector(isVisibleSelector, refreshObjectList);
		initObjectName(isSearchMode);
		
		if(LogIn.getInstance().isObjectFieldsLoaded())
			returnMe().setFields(createFormItem(LogIn.getInstance().getObjectFormFields(), isVisibleSelector, isSearchMode, isAddingMode));
		else {
			if (dataGetterAsc == null) {
				dataGetterAsc = GWT.create(DataGetterService.class);
			}
			AsyncCallback<List<AttributeDictDTO>> callback = new AsyncCallback<List<AttributeDictDTO>>() {
				public void onFailure(Throwable caught) {
					System.out.println(caught.getMessage());
				}
	
				public void onSuccess(List<AttributeDictDTO> result) {
					LogIn.getInstance().setObjectFormFields(result);
					returnMe().setFields(createFormItem(result, isVisibleSelector, isSearchMode, isAddingMode));
				}
			};
			dataGetterAsc.getAttributeList(true, callback);
		}
	}
	
	private FormItem[] createFormItem(List<AttributeDictDTO> attributeDictList, boolean isViewMode, boolean isSearchMode, boolean isAddingMode) {
		ArrayList<FormItem> objectFormItem = new ArrayList<FormItem>(attributeDictList.size() + 2);		
		
		//Pola standardowe
		if(isViewMode) {
			objectFormItem.add(objectSelector);
			objectName.disable();
		}
		
		objectFormItem.add(objectName);
		
		//Atrybuty
		for(final AttributeDictDTO attributeDict : attributeDictList) {
			if(attributeDict.getIsDate() == 0) {
				if(isSearchMode) {
					SelectItem tmpSelectItem = new SelectItem();
					tmpSelectItem.setDefaultValue("--- Wybierz z listy ---");
					tmpSelectItem.setValueMap(generateAttributeValuesToCombo(attributeDict.getAttributeValues()));
					tmpSelectItem.setVisible(true);
					tmpSelectItem.setName(attributeDict.getAttributeName().toLowerCase());
					tmpSelectItem.setWidth(130);
					tmpSelectItem.setHeight(18);
					tmpSelectItem.setTitle(attributeDict.getAttributeName().replace("_", " "));
					tmpSelectItem.setStartRow(true);
					tmpSelectItem.setEndRow(false);
					objectFormItem.add(tmpSelectItem);
				} else {
					TextItem tmpTextItem = new TextItem();
					tmpTextItem.setVisible(true);
					tmpTextItem.setName(attributeDict.getAttributeName().toLowerCase());
					tmpTextItem.setHeight(18);
					tmpTextItem.setTitle(attributeDict.getAttributeName().replace("_", " "));
					tmpTextItem.setStartRow(true);
					tmpTextItem.setEndRow(false);
					
					if(!isAddingMode)
						tmpTextItem.setWidth(250);
					else
						tmpTextItem.setWidth(130);
					
					if(attributeDict.getIsMultiple() == 0)
						tmpTextItem.setRequired(true);
						
					if (isViewMode && attributeDict.getIsMultiple() == 1)
						tmpTextItem.setVisible(false);
						
					objectFormItem.add(tmpTextItem);
				}
					
				if(isSearchMode)
					objectFormItem.add(createConfidenceCombo(attributeDict.getAttributeName().toLowerCase(), attributeDict.getIsMultiple()));
					
				if(attributeDict.getIsMultiple() == 1) {
					final ListGrid tmpListGrid;
					ListGridField iconField = new ListGridField("fileField", "Pliki/WWW");
					iconField.setWidth(100);
					if(attributeDict.getIsFile() == 0)
						tmpListGrid = new ListGrid();
					else  {					        
						tmpListGrid = new ListGrid() {
							protected Canvas createRecordComponent(final ListGridRecord record, Integer colNum) {
								String fieldName = this.getFieldName(colNum);
								
								if (fieldName.equals("fileField")) {
									HLayout recordCanvas = new HLayout(3);  
				                    recordCanvas.setHeight(22);  
				                    recordCanvas.setAlign(Alignment.CENTER);
				                    
				                    ImgButton pdfFile = new ImgButton();  
				                    pdfFile.setShowDown(false);  
				                    pdfFile.setShowRollOver(false);  
				                    pdfFile.setLayoutAlign(Alignment.CENTER);  
				                    pdfFile.setSrc("pdf-icon.png");
				                    pdfFile.setPrompt("Otwórz PDF");
				                    pdfFile.setHeight(16);  
				                    pdfFile.setWidth(16);                      
				                    pdfFile.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
										public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
											String articleName = record.getAttribute(attributeDict.getAttributeName().toLowerCase() + "_lg");
											openPdfFile(articleName);
										}
									});
				                 
				                    ImgButton wwwOpen = new ImgButton();  
				                    wwwOpen.setShowDown(false);  
				                    wwwOpen.setShowRollOver(false);  
				                    wwwOpen.setAlign(Alignment.CENTER);  
				                    wwwOpen.setSrc("www.jpg");  
				                    wwwOpen.setPrompt("View Chart");  
				                    wwwOpen.setHeight(16);  
				                    wwwOpen.setWidth(16);
				                    wwwOpen.addClickHandler(new com.smartgwt.client.widgets.events.ClickHandler() {
										public void onClick(com.smartgwt.client.widgets.events.ClickEvent event) {
											final String articleName = record.getAttribute(attributeDict.getAttributeName().toLowerCase() + "_lg");
											if (dataGetterAsc == null) {
												dataGetterAsc = GWT.create(DataGetterService.class);
											}
											AsyncCallback<String> callback = new AsyncCallback<String>() {
												public void onFailure(Throwable caught) {
													System.out.println(caught.getMessage());
												}
												public void onSuccess(String result) {
													if(result != null)
														Window.open(result, "New tab", "");
													else
														SC.say("Dla artykułu: " + articleName + " nie został zdefiniowany adres www");
												}
											};
												
											dataGetterAsc.getWebPageForArticle(articleName, callback);
										}
									});
					                    
				                    recordCanvas.addMember(pdfFile);  
				                    recordCanvas.addMember(wwwOpen);  
				                    return recordCanvas;
								} else
									return null;
							}
						};
					}
						
					final ListGridField tmpName = new ListGridField(attributeDict.getAttributeName().toLowerCase() + "_lg", attributeDict.getAttributeName().replace("_", " "));
					tmpName.setType(ListGridFieldType.TEXT);
					if(attributeDict.getIsFile() == 1)
						tmpName.setWidth(120);
					else
						tmpName.setWidth(200);
					
					if(!isViewMode) {
						final AttributeObject objectAttribute = new AttributeObject(tmpListGrid, attributeDict.getAttributeName().toLowerCase() + "_lg");
						objectsAttributeMap.put(attributeDict.getAttributeName().toLowerCase(), objectAttribute);
						
						ButtonItem tmpButtonItem = new ButtonItem();
						tmpButtonItem.setVisible(true);
						tmpButtonItem.setName(attributeDict.getAttributeName().toLowerCase() + "_bt");
						tmpButtonItem.setTitle("Dodaj");
						tmpButtonItem.setWidth(50);
						tmpButtonItem.setStartRow(false);
						tmpButtonItem.setEndRow(true);
						objectFormItem.add(tmpButtonItem);
						tmpButtonItem.addClickHandler(new ClickHandler() {
							public void onClick(ClickEvent event) {
								String newValue = returnMe().getField(attributeDict.getAttributeName().toLowerCase()).getDisplayValue();
								if(!newValue.isEmpty()) {
									objectAttribute.addNewValue(newValue);
									returnMe().getField(attributeDict.getAttributeName().toLowerCase()).clearValue();
								}
							}
						});
					}
					
					tmpListGrid.setShowAllRecords(true);
					if(!isSearchMode && !isAddingMode)
						tmpListGrid.setWidth(250);
					else
						tmpListGrid.setWidth(130);
					tmpListGrid.setHeight(80);
					
					if(attributeDict.getIsFile() == 0)
						tmpListGrid.setFields(tmpName);
					else {
						tmpListGrid.setShowRecordComponents(true);          
						tmpListGrid.setShowRecordComponentsByCell(true);
						tmpListGrid.setFields(tmpName, iconField);
						tmpListGrid.setCanResizeFields(true);
					}
						
					CanvasItem tmpCanvasItem = new CanvasItem();
					tmpCanvasItem.setName(attributeDict.getAttributeName().toLowerCase() + "_ci");
					tmpCanvasItem.setCanvas(tmpListGrid);
					
					if(isViewMode)
						tmpCanvasItem.setTitle(attributeDict.getAttributeName().replace("_", " "));
					else
						tmpCanvasItem.setTitle("");
					
					objectFormItem.add(tmpCanvasItem);
				}
			} else {
				DateItem tmpDateItem = new DateItem();
			    tmpDateItem.setUseTextField(true);
			    tmpDateItem.setName(attributeDict.getAttributeName().toLowerCase());
			    if(!isSearchMode)
			    	tmpDateItem.setRequired(true);
			    tmpDateItem.setTitle(attributeDict.getAttributeName().replace("_", " "));
			    tmpDateItem.setDisplayFormat(DateDisplayFormat.TOEUROPEANSHORTDATE);
			    tmpDateItem.setHeight(18);
			    tmpDateItem.setWidth(130);
			    objectFormItem.add(tmpDateItem);
				    
			    if(isSearchMode)
					objectFormItem.add(createConfidenceCombo(attributeDict.getAttributeName().toLowerCase(), attributeDict.getIsMultiple()));
			}
		}
		
		return objectFormItem.toArray(new FormItem[objectFormItem.size()]);
	}
	
	private SelectItem createConfidenceCombo(String attributeName, int isEndRow) {
		
		ArrayList<String> confidence = new ArrayList<String>();
		confidence.add("Pewny");
		confidence.add("Raczej pewny");
		confidence.add("Raczej niepewny");
		confidence.add("Niepewny");
		
		SelectItem selectItem = new SelectItem();
		selectItem.setName("confidenceSelectItem_" + attributeName);
		selectItem.setDefaultToFirstOption(true);
		selectItem.setVisible(true);
		selectItem.setShowTitle(false);
		if(isEndRow == 0)
			selectItem.setEndRow(true);
		else
			selectItem.setEndRow(false);
		selectItem.setHeight(18);
		selectItem.setWidth(110);
		selectItem.setValueMap(confidence.toArray(new String[confidence.size()]));
		
		return selectItem;
	}
	
	private String[] generateAttributeValuesToCombo( Set<AttributeValuesDictDTO> attributeValuesDTO ) {
		ArrayList<String> objectAttributes = new ArrayList<String>();
		if(attributeValuesDTO != null) {
			for(AttributeValuesDictDTO attributeValues : attributeValuesDTO) {
				objectAttributes.add(attributeValues.getAttributeValue());
			}
		}
		
		return objectAttributes.toArray(new String[objectAttributes.size()]);
	}
	
	private void openPdfFile(String fileName) {
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<String> callback = new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(String result) {
				if(!result.equals("OK"))
					SC.say(result);
			}
		};
		
		dataGetterAsc.openPdfFile(fileName, callback);
	}
	
	public void refreshObjectList(final SelectItem objectSelector, final Long ident) {
		
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<List<ObjectsDTO>> callback = new AsyncCallback<List<ObjectsDTO>>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(List<ObjectsDTO> result) {
				objectsList = result;
				objectSelector.setValueMap(generateObjectsToCombo(objectsList));
			}
		};
		
		dataGetterAsc.getListOfAllObjects(callback);
	}
	
	private LinkedHashMap<String, String> generateObjectsToCombo( List<ObjectsDTO> objectsDTO ) {
		LinkedHashMap<String, String> retVal = new LinkedHashMap<String, String>();
		if(objectsDTO != null) {
			for(ObjectsDTO objects : objectsDTO) {
				retVal.put("" + objects.getId(), objects.getName());
			}
		}
		
		return retVal;
	}
	
	private ObjectsDTO getSelectedObjects(Long id, List<ObjectsDTO> objectsDTO) {
		if(objectsDTO != null) {
			for(ObjectsDTO objects : objectsDTO ) {
				if(objects.getId().equals(id)) {
					return objects;
				}
			}
		}
		return null;
	}

	private void initObjectName(boolean isSearchMode) {
		LengthRangeValidator validator = new LengthRangeValidator();
        validator.setMin(1);
        this.objectName.setVisible(false);
        this.objectName.setValidators(validator);
		this.objectName.setName("objectName");
		if(!isSearchMode)
			this.objectName.setRequired(true);
		this.objectName.setTitle("objectName");
		properties.getFormItemTitle("objectName", objectName);
		this.objectName.setHeight(18);
		this.objectName.setWidth(130);
	}
	
	private void initObjectSelector(boolean isVisible, boolean refreshObjectList)
	{
		if(isVisible) {
			objectSelector.setVisible(true);
			objectSelector.setName("combo");
			objectSelector.setDefaultValue("--- Wybierz z listy ---");
			objectSelector.setWidth(130);
			objectSelector.setHeight(18);
			
			objectSelector.setTitle("objectSelector");
			properties.getFormItemTitle("objectSelector", objectSelector);
			objectSelector.addChangedHandler( new ChangedHandler() {
				public void onChanged(ChangedEvent event) {
					selectedObject = getSelectedObjects(Long.valueOf((String)objectSelector.getValue()).longValue(), objectsList);
					if(selectedObject != null) {
						returnMe().getField("objectName").setValue(selectedObject.getName());
						getObjectAttributes(Long.valueOf((String)objectSelector.getValue()).longValue());

						if(objectHandling != null && LogIn.getInstance().getUser().getAppId() == LogIn.SEARCH_APP)
							objectHandling.refreshObjectHistory(selectedObject.getId());
						else
							objectHandling.refreshObjectEstimates(selectedObject.getId());
					}
				}
			});
			if(refreshObjectList)
				refreshObjectList(objectSelector, null);
		}
		else
			objectSelector.setVisible(false);
	}
	
	public void setObjectList(List<ObjectsDTO> objectList) {
		this.objectsList = objectList;
	}
	
	public SelectItem getObjectSelection() {
		return objectSelector;
	}
	
	public ObjectsDTO getSelectedObject() {
		return this.selectedObject;
	}
	
	private ObjectDetailsForm returnMe() {
		return this;
	}
	
	public void setObjectHandling(ObjectHandling objectHandling) {
		this.objectHandling = objectHandling;
	}
	
	public void clearForm() {
		this.clearValues();
		List<FormItem> formItems = Arrays.asList(this.getFields());
		for(FormItem formItem : formItems) {
			if(formItem instanceof CanvasItem) {
				if(((CanvasItem) formItem).getFieldName().contains("_ci")) {
					AttributeObject test = objectsAttributeMap.get(((CanvasItem) formItem).getFieldName().replace("_ci", ""));
					if(test!=null)
						test.clearValues();
					ListGrid tmpListGrid = (ListGrid)((CanvasItem) formItem).getCanvas();
					if(tmpListGrid != null) {
						tmpListGrid.selectAllRecords();
						tmpListGrid.removeSelectedData();
					}
				}
			}
		}
	}
	
	public void getObjectAttributes(Long objectId) {
		if(this.isVisible())
			clearForm();
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<Set<ObjectAttributesDTO>> callback = new AsyncCallback<Set<ObjectAttributesDTO>>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(Set<ObjectAttributesDTO> result) {
				for(ObjectAttributesDTO objectAttributes : result) {
					if( returnMe().getField(objectAttributes.getAttribute()) instanceof SelectItem ) {
						SelectItem tmpSelectItem = (SelectItem) returnMe().getField(objectAttributes.getAttribute());
						tmpSelectItem.setValue(objectAttributes.getAttributeValue());
					}
					else if( returnMe().getField(objectAttributes.getAttribute()) instanceof TextItem && returnMe().getField(objectAttributes.getAttribute() + "_ci") == null){
						TextItem tmpTextItem = (TextItem) returnMe().getField(objectAttributes.getAttribute());
						tmpTextItem.setValue(objectAttributes.getAttributeValue());
					}
					else if( returnMe().getField(objectAttributes.getAttribute()) instanceof DateItem ) {
						DateItem tmpDateItem = (DateItem) returnMe().getField(objectAttributes.getAttribute());
						tmpDateItem.setValue(objectAttributes.getAttributeValue());
					}
					else if( returnMe().getField(objectAttributes.getAttribute() + "_ci") instanceof CanvasItem ) {
						ListGrid tmpListGridItem = ((ListGrid)((CanvasItem) returnMe().getField(objectAttributes.getAttribute() + "_ci")).getCanvas());
						ListGridRecord newRecord = new ListGridRecord();
						newRecord.setAttribute(objectAttributes.getAttribute() + "_lg", objectAttributes.getAttributeValue());
						ArrayList<ListGridRecord> tmpArrayList = new ArrayList<ListGridRecord>(Arrays.asList(tmpListGridItem.getRecords()));
						tmpArrayList.add(newRecord);
						tmpListGridItem.setData(tmpArrayList.toArray(new ListGridRecord[tmpArrayList.size()]));	
					}
				}
			}
		};
		
		dataGetterAsc.getObjectAttributes(objectId, callback);
	}
	
	public void getHistoryObjectAttributes(Long historyId) {
		clearForm();
		if (dataGetterAsc == null) {
			dataGetterAsc = GWT.create(DataGetterService.class);
		}
		
		AsyncCallback<List<SearchResultAttributesDTO>> callback = new AsyncCallback<List<SearchResultAttributesDTO>>() {
			public void onFailure(Throwable caught) {
				System.out.println(caught.getMessage());
			}

			public void onSuccess(List<SearchResultAttributesDTO> result) {
				for(SearchResultAttributesDTO searchHistoryResult : result) {
					if( returnMe().getField(searchHistoryResult.getAttributeName()) instanceof SelectItem ) {
						SelectItem tmpSelectItem = (SelectItem) returnMe().getField(searchHistoryResult.getAttributeName());
						tmpSelectItem.setValue(searchHistoryResult.getAttributeValue());
					}
					else if( returnMe().getField(searchHistoryResult.getAttributeName()) instanceof TextItem && returnMe().getField(searchHistoryResult.getAttributeName() + "_ci") == null){
						TextItem tmpTextItem = (TextItem) returnMe().getField(searchHistoryResult.getAttributeName());
						tmpTextItem.setValue(searchHistoryResult.getAttributeValue());
					}
					else if( returnMe().getField(searchHistoryResult.getAttributeName()) instanceof DateItem ) {
						DateItem tmpDateItem = (DateItem) returnMe().getField(searchHistoryResult.getAttributeName());
						tmpDateItem.setValue(searchHistoryResult.getAttributeValue());
					}
					else if( returnMe().getField(searchHistoryResult.getAttributeName() + "_ci") instanceof CanvasItem ) {
						ListGrid tmpListGridItem = ((ListGrid)((CanvasItem) returnMe().getField(searchHistoryResult.getAttributeName() + "_ci")).getCanvas());
						ListGridRecord newRecord = new ListGridRecord();
						newRecord.setAttribute(searchHistoryResult.getAttributeName() + "_lg", searchHistoryResult.getAttributeValue());
						ArrayList<ListGridRecord> tmpArrayList = new ArrayList<ListGridRecord>(Arrays.asList(tmpListGridItem.getRecords()));
						tmpArrayList.add(newRecord);
						tmpListGridItem.setData(tmpArrayList.toArray(new ListGridRecord[tmpArrayList.size()]));	
					}
				}
			}
		};
		
		dataGetterAsc.getHistorySearchAttributes(historyId, callback);
	}
}

class AttributeObjectRecord extends ListGridRecord {

	AttributeObjectRecord() {}
	AttributeObjectRecord(String name, String fieldName) {
		setName(fieldName, name);
	}
	
	public String getName(String fieldName) {
		return getAttributeAsString(fieldName);
	}
	public void setName(String fieldName, String name) {
		setAttribute(fieldName, name);
	}
}

class AttributeObject {
	private ListGrid list;
	private String fieldName;
	private ArrayList<AttributeObjectRecord> records = new ArrayList<AttributeObjectRecord>();
	
	AttributeObject(ListGrid list, String fieldName) {
		this.list = list;
		this.fieldName = fieldName;
	}
	
	public void addNewValue(String value) {
		records.add(new AttributeObjectRecord(value, fieldName));
		list.setData(records.toArray(new AttributeObjectRecord[records.size()]));
	}
	
	public void clearValues() {
		records.clear();
	}
}
